
package sopraim.presenter.services;

import sopraim.entity.User;
import java.util.ArrayList;
import javax.jws.WebMethod;
import javax.jws.WebService;
import javax.jws.WebParam;
import sopraim.model.AdminDAO;
import sopraim.model.Model;
import sopraim.model.UserDAO;
import sopraim.presenter.services.util.Utils;
import sopraim.presenter.servlets.ConnectionServlet;
import sopraim.presenter.validate.AddUserForm;
import sopraim.presenter.validate.DeleteUserForm;

/**
 * Admin web services endpoint
 * @author INFO-A1
 */
@WebService( serviceName = "AdminService")
public class AdminService {
    private final Model model = Model.getInstance();
    private final AdminDAO adminDao = model.getAdminDAO();
    private final UserDAO userDao = model.getUserDAO();
    
    /**
     * set admin rights
     * @param admin the user name of the admin who want to grant or remove admin rights
     * @param target the username of the user to promote/remove admin rights
     * @param promote true if the user will be promoted admin, false otherwise
     * @throws Exception
     */
    @WebMethod(operationName = "setRights")
    public void setRights(@WebParam(name = "admin")String admin, @WebParam(name = "target")
            String target, @WebParam(name = "promote")boolean promote) throws Exception{
        if(promote){
            adminDao.addAdminRight(userDao.findByLogin(target));
        }
        else{
            adminDao.removeAdminRight(userDao.findByLogin(target));
        }
    }
    
    /**
     * create an account
     * @param admin the account creator username, must be an admin
     * @param username
     * @param email
     * @param name
     * @param firstName
     * @param isAdmin
     * @param location
     * @throws java.lang.Exception
     */
    @WebMethod(operationName = "createAccount")
    public void createAccount(String admin, String username, String email, String name, String firstName, boolean isAdmin,
            String location) throws Exception{
        //validate form and check admin
        AddUserForm form = new AddUserForm(admin, username, name, firstName, email);
        adminDao.addUser(username, name, firstName, username, email, isAdmin);
        try{
            User user = userDao.findByLogin(username);
            Utils utils = new Utils();
            utils.sendMail(user,ConnectionServlet.url);
        }
        catch(Exception e){
            System.err.println(e.getMessage());
        }
    }
    
    /**
     * delete an account
     * @param admin the user who is performing the account  deletion , must be an admin
     * @param toDelete the username of the account to delete
     * @throws Exception
     */
    @WebMethod(operationName = "deleteAccount")
    public void deleteAccount(@WebParam(name = "admin")String admin, @WebParam(name = "toDelete")
            String toDelete) throws Exception{
        DeleteUserForm form= new DeleteUserForm(admin, toDelete);
        adminDao.removeUser(userDao.findByLogin(toDelete));
        
    }
    
    /**
     * get all the registered accounts
     * @param admin the username of the user who requested the user list
     * @return the user list
     */
    @WebMethod(operationName ="getUserList")
    public ArrayList<User> getUserList(@WebParam(name = "admin") String admin){
        return null;
    }
}
